/**
 * Copyright (C), 2015-2024, XXX有限公司
 * FileName: BinarySerch
 * Author:   Administrator
 * Date:     2024/3/24 10:39
 * Description:
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */

/**
 * 〈一句话功能简述〉<br>
 * 〈〉
 *
 * @author Administrator
 * @create 2024/3/24
 * @since 1.0.0
 */
public class BinarySearch {


    public static void main(String[] args) {
        int arr[] = {1, 2, 3, 4, 5, 6, 7, 8};
        int target = 5;
        int result = binarySearch(arr, target, 0, arr.length - 1);
        System.out.println(result);
    }

    public static int binarySearch(int[] arr, int target, int start, int last) {

        int result = -1;
        int mid = (start + last) / 2;
        if (arr[mid] == target) {
            result = mid;
        } else if (arr[mid] > target) {
            return binarySearch(arr, target, start, mid - 1);
        } else {
            return binarySearch(arr, target, mid + 1, last);
        }
        return result;
    }
}